package com.meihua.baoleyi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.meihua.baoleyi.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by liming on 2017/10/17.
 */

public class ClearCacheDialog extends Dialog{
    @Bind(R.id.tv_dlg_msg)
    TextView tvMsg;
    @Bind(R.id.tv_dlg_yes)
    TextView tvYes;
    @Bind(R.id.tv_dlg_no)
    TextView tvNo;
    private Context mContext;
    private String mMessage;
    private String mPositiveButtonName;
    private OnClickListener mPositiveListener;
    private String mNativeButtonName;
    private OnClickListener mNativeListener;

    public ClearCacheDialog(Context context,
                            String message,
                            String positiveButtonName,
                            OnClickListener positiveListener,
                            String nativeButtonName,
                            OnClickListener nativeListener) {
        super(context,R.style.dialog);
        mContext = context;
        mMessage = message;
        mPositiveButtonName = positiveButtonName;
        mPositiveListener = positiveListener;
        mNativeButtonName = nativeButtonName;
        mNativeListener = nativeListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_clear_cache);
        ButterKnife.bind(this);
        settings();
    }

    private void settings() {
        setMessage();
        setPositiveButton();
        setNativeButton();
    }

    private void setMessage() {
        if(!TextUtils.isEmpty(mMessage)){
            tvMsg.setText(mMessage);
        }
    }

    private void setNativeButton() {

        if(!TextUtils.isEmpty(mNativeButtonName)){
            tvNo.setText(mNativeButtonName);
        }
    }

    private void setPositiveButton() {

        if(!TextUtils.isEmpty(mPositiveButtonName)){
            tvYes.setText(mPositiveButtonName);
        }
    }

    @OnClick({R.id.tv_dlg_yes,R.id.tv_dlg_no})
    public void onViewClicked(View v){
        switch (v.getId()) {
            case R.id.tv_dlg_yes:
                if(mPositiveListener != null){
                    mPositiveListener.onClick(ClearCacheDialog.this,v.getId());
                }
                break;
            case R.id.tv_dlg_no:
                if(mNativeListener != null){
                    mNativeListener.onClick(ClearCacheDialog.this,v.getId());
                }
                break;
        }
    }

    public static class Builder {
        private Context mContext;
        private String message;
        private String mPositiveButtonName;
        private OnClickListener mPositiveListener;
        private String mNativeButtonName;
        private OnClickListener mNativeListener;

        public Builder(Context context){
            mContext = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setPositiveButton(String s,OnClickListener l){
            mPositiveButtonName = s;
            mPositiveListener = l;
            return this;
        }

        public Builder setNativeButton(String s,OnClickListener l){
            mNativeButtonName = s;
            mNativeListener = l;
            return this;
        }

        public ClearCacheDialog build(){
            ClearCacheDialog dlg = new ClearCacheDialog(
                    mContext,
                    message,
                    mPositiveButtonName,
                    mPositiveListener,
                    mNativeButtonName,
                    mNativeListener);
            return dlg;
        }
        public void show(){
            ClearCacheDialog dlg = new ClearCacheDialog(
                    mContext,
                    message,
                    mPositiveButtonName,
                    mPositiveListener,
                    mNativeButtonName,
                    mNativeListener);
            dlg.show();
        }
    }
}
